<template>
  <!-- 周转卡表表单弹窗 -->
  <el-dialog
    v-model="visibleDialog"
    :title="props.title"
    width="70%"
    draggable
    @close="closeDialog"
  >
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="160">
      <el-row>
        <el-col :span="12">
          <el-form-item label="公司id" prop="organizationId">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.organizationId"
              placeholder="请输入公司id"
              clearable
            />
            <span v-else>{{ formData.organizationId }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="公司编号" prop="organizationCode">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.organizationCode"
              placeholder="请输入公司编号"
              clearable
            />

            <span v-else>{{ formData.organizationCode }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="工厂id" prop="plantId">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.plantId"
              placeholder="请输入工厂id"
              clearable
            />

            <span v-else>{{ formData.plantId }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="工厂编号" prop="plantCode">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.plantCode"
              placeholder="请输入工厂编号"
              clearable
            />

            <span v-else>{{ formData.plantCode }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="周转卡编号" prop="turnoverCode">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.turnoverCode"
              placeholder="请输入周转卡编号"
              clearable
            />

            <span v-else>{{ formData.turnoverCode }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="物料id" prop="materialId">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.materialId"
              placeholder="请输入物料id"
              clearable
            />

            <span v-else>{{ formData.materialId }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="物料编码" prop="materialCode">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.materialCode"
              placeholder="请输入物料编码"
              clearable
            />

            <span v-else>{{ formData.materialCode }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="物料名称" prop="materialName">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.materialName"
              placeholder="请输入物料名称"
              clearable
            />

            <span v-else>{{ formData.materialName }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="图号" prop="figureNumber">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.figureNumber"
              placeholder="请输入图号"
              clearable
            />

            <span v-else>{{ formData.figureNumber }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="工单id" prop="workingFormId">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.workingFormId"
              placeholder="请输入工单id"
              clearable
            />

            <span v-else>{{ formData.workingFormId }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="条码状态" prop="status">
            <el-select
              v-if="!props.isReadonly"
              v-model="formData.status"
              placeholder="请选择条码状态"
            >
              <el-option
                v-for="item in status"
                :key="item.code"
                :label="item.name"
                :value="item.code"
              />
            </el-select>
            <span v-else>{{ processedStatus }}</span>

            <!-- <el-input
              v-if="!props.isReadonly"
              v-model="formData.status"
              placeholder="请输入条码状态;1、初始打印2、报工关联"
              clearable
            />
            <span v-else>{{ formData.status }}</span> -->
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="工作中心id" prop="workCenterId">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.workCenterId"
              placeholder="请输入工作中心id"
              clearable
            />

            <span v-else>{{ formData.workCenterId }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="工作中心编号" prop="workCenterCode">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.workCenterCode"
              placeholder="请输入工作中心编号"
              clearable
            />

            <span v-else>{{ formData.workCenterCode }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="工作中心名称" prop="workCenterName">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.workCenterName"
              placeholder="请输入工作中心名称"
              clearable
            />

            <span v-else>{{ formData.workCenterName }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="线边库id" prop="produceStorageId">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.produceStorageId"
              placeholder="请输入线边库id"
              clearable
            />

            <span v-else>{{ formData.produceStorageId }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="工序主键" prop="processesId">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.processesId"
              placeholder="请输入工序主键"
              clearable
            />

            <span v-else>{{ formData.processesId }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="工序分类" prop="processesClassification">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.processesClassification"
              placeholder="请输入工序分类"
              clearable
            />

            <span v-else>{{ formData.processesClassification }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="数量" prop="materialQuantity">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.materialQuantity"
              placeholder="请输入数量"
              clearable
            />

            <span v-else>{{ formData.materialQuantity }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="数量单位" prop="quantityUnit">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.quantityUnit"
              placeholder="请输入数量单位"
              clearable
            />

            <span v-else>{{ formData.quantityUnit }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="周转卡类型" prop="turnoverType">
            <el-select
              v-if="!props.isReadonly"
              v-model="formData.turnoverType"
              placeholder="请选择周转卡类型"
            >
              <el-option
                v-for="item in cardstatus"
                :key="item.code"
                :value="item.code"
                :label="item.name"
              />
            </el-select>
            <span v-else>{{ processedValue }}</span>
            <!-- <el-input
              v-if="!props.isReadonly"
              v-model="formData.turnoverType"
              placeholder="请输入周转卡类型;1、合格、2、不合格、3、返修"
              clearable
            />

            <span v-else>{{ formData.turnoverType }}</span> -->
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="批次号" prop="materialBatch">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.materialBatch"
              placeholder="请输入批次号"
              clearable
            />

            <span v-else>{{ formData.materialBatch }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="物料条码" prop="materialBarcode">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.materialBarcode"
              placeholder="请输入物料条码"
              clearable
            />

            <span v-else>{{ formData.materialBarcode }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="数据版本号" prop="version">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.version"
              placeholder="请输入数据版本号"
              clearable
            />

            <span v-else>{{ formData.version }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="备注" prop="remarks">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.remarks"
              placeholder="请输入备注"
              clearable
            />

            <span v-else>{{ formData.remarks }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="创建时间" prop="createTime">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.createTime"
              placeholder="请输入创建时间"
              clearable
            />

            <span v-else>{{ formData.createTime }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="创建人" prop="createdUserId">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.createdUserId"
              placeholder="请输入创建人"
              clearable
            />

            <span v-else>{{ formData.createdUserId }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="创建人名" prop="createdUserName">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.createdUserName"
              placeholder="请输入创建人名"
              clearable
            />

            <span v-else>{{ formData.createdUserName }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="更新时间" prop="lastUpdateTime">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.lastUpdateTime"
              placeholder="请输入更新时间"
              clearable
            />

            <span v-else>{{ formData.lastUpdateTime }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="更新人" prop="lastUpdatedUserId">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.lastUpdatedUserId"
              placeholder="请输入更新人"
              clearable
            />

            <span v-else>{{ formData.lastUpdatedUserId }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="更新人名" prop="lastUpdatedUserName">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.lastUpdatedUserName"
              placeholder="请输入更新人名"
              clearable
            />

            <span v-else>{{ formData.lastUpdatedUserName }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="删除标识" prop="deleted">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.deleted"
              placeholder="请输入删除标识"
              clearable
            />

            <span v-else>{{ formData.deleted }}</span>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button
          v-if="!props.isReadonly"
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >确 定</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "MESTURNOVERCARDEDIT",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
    console.log("newVal");
    console.log(newVal);
    setTimeout(() => {
      formRef.value.clearValidate();
    }, 50);
  }
);

const rules = reactive({
  organizationId: [
    {
      required: true,
      min: 1,
      max: 32,
      message: "长度在 1 到 32 个字符",
      trigger: "blur",
    },
  ],
  organizationCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  plantId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  plantCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  turnoverCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  materialId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  materialCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  materialName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  figureNumber: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  workingFormId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  status: [],
  workCenterId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  workCenterCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  workCenterName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  produceStorageId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  processesId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  processesClassification: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  materialQuantity: [],
  quantityUnit: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  turnoverType: [],
  materialBatch: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  materialBarcode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  version: [],
  remarks: [
    { min: 1, max: 255, message: "长度在 1 到 255 个字符", trigger: "blur" },
  ],
  createTime: [],
  createdUserId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  createdUserName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  lastUpdateTime: [],
  lastUpdatedUserId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  lastUpdatedUserName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  deleted: [],
});

const formRef = ref(ElForm);

let formData = reactive({
  id: "",
  organizationId: "",
  organizationCode: "",
  plantId: "",
  plantCode: "",
  turnoverCode: "",
  materialId: "",
  materialCode: "",
  materialName: "",
  figureNumber: "",
  workingFormId: "",
  status: "",
  workCenterId: "",
  workCenterCode: "",
  workCenterName: "",
  produceStorageId: "",
  processesId: "",
  processesClassification: "",
  materialQuantity: "",
  quantityUnit: "",
  turnoverType: "",
  materialBatch: "",
  materialBarcode: "",
  version: "",
  remarks: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
  deleted: "",
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      const {
        id,
        organizationId,
        organizationCode,
        plantId,
        plantCode,
        turnoverCode,
        materialId,
        materialCode,
        materialName,
        figureNumber,
        workingFormId,
        status,
        workCenterId,
        workCenterCode,
        workCenterName,
        produceStorageId,
        processesId,
        processesClassification,
        materialQuantity,
        quantityUnit,
        turnoverType,
        materialBatch,
        materialBarcode,
        version,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        deleted,
      } = newVal;
      Object.assign(formData, {
        id,
        organizationId,
        organizationCode,
        plantId,
        plantCode,
        turnoverCode,
        materialId,
        materialCode,
        materialName,
        figureNumber,
        workingFormId,
        status,
        workCenterId,
        workCenterCode,
        workCenterName,
        produceStorageId,
        processesId,
        processesClassification,
        materialQuantity,
        quantityUnit,
        turnoverType,
        materialBatch,
        materialBarcode,
        version,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        deleted,
      });
    }
    console.log("watch", formData.turnoverType, formData.status);
    formData.turnoverType = chooseTurnoverType();
    formData.status = chooseStatus();
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.id = "";
  formData.organizationId = "";
  formData.organizationCode = "";
  formData.plantId = "";
  formData.plantCode = "";
  formData.turnoverCode = "";
  formData.materialId = "";
  formData.materialCode = "";
  formData.materialName = "";
  formData.figureNumber = "";
  formData.workingFormId = "";
  formData.status = "";
  formData.workCenterId = "";
  formData.workCenterCode = "";
  formData.workCenterName = "";
  formData.produceStorageId = "";
  formData.processesId = "";
  formData.processesClassification = "";
  formData.materialQuantity = "";
  formData.quantityUnit = "";
  formData.turnoverType = "";
  formData.materialBatch = "";
  formData.materialBarcode = "";
  formData.version = "";
  formData.remarks = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.createdUserName = "";
  formData.lastUpdateTime = "";
  formData.lastUpdatedUserId = "";
  formData.lastUpdatedUserName = "";
  formData.deleted = "";
}

const loading = ref(false);

/** 周转卡表保存提交 */
function handleSubmit() {
  console.log("formRef", formRef);

  formRef.value.validate(async (valid: any) => {
    if (valid) {
      console.log("valid", valid);
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      await request({
        url: "/mes/mesturnovercard/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});

const status = [
  { code: "1", name: "初始打印" },
  { code: "2", name: "报工关联" },
];

const cardstatus = [
  { code: "1", name: "合格", value: "1" },
  { code: "2", name: "不合格", value: "2" },
  { code: "3", name: "返修", value: "3" },
];

let processedValue = computed(() => {
  return chooseTurnoverType();
});
function chooseTurnoverType() {
  for (let card of cardstatus) {
    if (card.code == formData.turnoverType) {
      return card.name;
    }
  }
  return formData.turnoverType;
}

let processedStatus = computed(() => {
  return chooseStatus();
});
function chooseStatus() {
  for (let card of status) {
    if (card.code == formData.status) {
      return card.name;
    }
  }
  return formData.status;
}
</script>
